﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain;
using Microsoft.Xna.Framework;

namespace Demos
{
    public class SpinningCylinder2D : DemoScreen, IComparer<Sprite>
    {
        public List<Sprite> Sprites;

        Rectangle Initial;
        public SpinningCylinder2D()
        {
            Sprites = new List<Sprite>();

            for (int i = 0; i < 10; i++)
            {
                Sprite sprite = new Sprite();
                sprite.TexturePlace = @"Game\Textures\farseerlogo";

                sprite.BoundsRectangleColor = ColorUtil.Random();
                

                this.AddComponent(sprite);
                Sprites.Add(sprite);

                sprite.UpdateBoundsRectangle();
                Initial = sprite.BoundsRectangle;
            }
        }

        public int CurrentIndex = 3;

        public int Compare(Sprite x, Sprite y)
        {
            int index = Sprites.IndexOf(y);
            if (index > CurrentIndex)
            {
                return 0;
            }
            else
            {
                return 1;
            }

            int index2 = Sprites.IndexOf(y);
            
        }
        
        public override void Update()
        {
            base.Update();

            Rectangle view = Engine.Instance.GraphicsDevice.Viewport.Bounds;
            Rectangle last = Rectangle.Empty;

            Sprite center = null;
            Sprite after1 = null;
            //Sprites.Sort(CurrentIndex, Sprites.Count - CurrentIndex, this); 
            for (int i = 0; i < Sprites.Count; i++)
            {
                Sprite sprite = Sprites[i];

                if (i == CurrentIndex - 2)
                {
                    sprite.BoundsRectangle = RectangleUtil.Scale(Initial, 0.25f);
                    sprite.BoundsRectangle = RectangleUtil.CenterRectangle(
                        sprite.BoundsRectangle,
                        new Rectangle(0,
                            0,
                            view.Width - Initial.Width - (int)(Initial.Width * 0.5f),
                            view.Height));
                    sprite.Rendering = SpriteRendering.Rendered;
                }
                else if (i == CurrentIndex - 1)
                {
                    sprite.BoundsRectangle = RectangleUtil.Scale(Initial, 0.5f);
                    sprite.BoundsRectangle = RectangleUtil.CenterRectangle(
                        sprite.BoundsRectangle, new Rectangle(0, 0, view.Width - Initial.Width, view.Height));
                    sprite.Rendering = SpriteRendering.Rendered;
                }
                else if (i == CurrentIndex)
                {
                    sprite.BoundsRectangle = RectangleUtil.CenterRectangle(
                        sprite.BoundsRectangle, view);
                    sprite.Rendering = SpriteRendering.Rendered;
                    last = sprite.BoundsRectangle;
                    center = sprite;
                }
                else if (i == CurrentIndex + 1)
                {
                    sprite.BoundsRectangle = RectangleUtil.Scale(Initial, 0.5f);
                    sprite.BoundsRectangle = RectangleUtil.CenterRectangle(
                        sprite.BoundsRectangle, new Rectangle(0, 0, view.Width - Initial.Width, view.Height));
                    sprite.BoundsRectangle =
                        new Rectangle(last.X + last.Width - (sprite.BoundsRectangle.Width / 2),
                            sprite.BoundsRectangle.Height, sprite.BoundsRectangle.Width, sprite.BoundsRectangle.Height);
                    sprite.Rendering = SpriteRendering.Rendered;
                    last = sprite.BoundsRectangle;
                    after1 = sprite;
                }
                else if (i == CurrentIndex + 2)
                {
                    sprite.BoundsRectangle = RectangleUtil.Scale(Initial, 0.25f);
                    sprite.BoundsRectangle = RectangleUtil.CenterRectangle(
                        sprite.BoundsRectangle,
                        new Rectangle(0,
                            0,
                            view.Width - Initial.Width - (int)(Initial.Width * 0.5f),
                            view.Height));
                    sprite.BoundsRectangle =
                        new Rectangle(last.X + last.Width - (sprite.BoundsRectangle.Width / 2),
                            sprite.BoundsRectangle.Y, sprite.BoundsRectangle.Width, sprite.BoundsRectangle.Height);
                    sprite.Rendering = SpriteRendering.Rendered;
                }
                else
                {
                    sprite.Rendering = SpriteRendering.Invisible;
                }
            }

            if (after1 != null)
            {
                after1.Rendering = SpriteRendering.Invisible;
                after1.Rendering = SpriteRendering.Rendered;
            }

            if (center != null)
            {
                center.Rendering = SpriteRendering.Invisible;
                center.Rendering = SpriteRendering.Rendered;
            }
        }

    }
}
